package com.alibaba.citrus.cr.mn.order.center.facade.adapter.impl;

import com.alibaba.citrus.cr.mn.order.center.facade.adapter.BaseDataServiceAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.convert.BaseDataItemServiceAdapterConvertor;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.dataobject.request.SearchScItemQueryRequest;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.dataobject.response.BaseDataScItemDTO;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcorder.model.dto.*;
import com.epoch.app.bcorder.service.BaseDataService;
import com.epoch.app.mnordercenter.model.dto.BatchQueryEmployeeByConditionRequest;
import com.epoch.app.mnordercenter.model.dto.CommonDictionarySelectRequest;
import com.epoch.app.mnordercenter.model.dto.DictionaryResponse;
import com.epoch.app.mnordercenter.model.dto.Modifiers;
import com.epoch.app.mnordercenter.service.MnOrderCenterService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author zhangxiaorui
 * @ClassName BaseDataItemServiceAdapterImpl
 * @description: TODO
 * @date 2023/05/09 11:13
 * @version: 1.0
 */
@Service
public class BaseDataServiceAdapterImpl implements BaseDataServiceAdapter {
    private static final Log log = Log.getLogger(BaseDataServiceAdapterImpl.class);

    @Resource
    private BaseDataService baseDataService;
    @Resource
    private MnOrderCenterService mnOrderCenterService;

    @Override
    public List<BaseDataScItemDTO> searchScItemByDBWhenDegraded(SearchScItemQueryRequest searchScItemRequest) {

        SearchScItemByDBWhenDegradedRequest request = new SearchScItemByDBWhenDegradedRequest();

        request.setPageNum(searchScItemRequest.getPageNum());
        request.setPageSize(searchScItemRequest.getPageSize());
        request.setSearchReqDTO(searchScItemRequest.getSearchReqDTO());
        request.setBizId(searchScItemRequest.getBizId());
        request.setMerchantId(searchScItemRequest.getMerchantId());
        request.setOperatorId(searchScItemRequest.getOperatorId());
        request.setOperatorType(searchScItemRequest.getOperatorType());

        Result<List<ScItemSDO>> listResult = baseDataService.searchScItemByDBWhenDegraded(request);

        return BaseDataItemServiceAdapterConvertor.convert(listResult);
    }

    @Override
    public Map<String, String> baselineGetOrganizationByCodes2(List<String> codes) {
        if(CollectionUtils.isEmpty(codes)) {
            return Maps.newHashMap();
        }
        BaselineGetOrganizationByCodes2Request request = new BaselineGetOrganizationByCodes2Request();
        request.setIds(codes);
        request.setStatus(1);
        BaselineGetOrganizationByCodes2Response response = baseDataService.baselineGetOrganizationByCodes2(request);
        if (Objects.isNull(response) || CollectionUtils.isEmpty(response.getResult())) {
            return Maps.newHashMap();
        }

        Map<String, String> codeIdMap = new HashMap<>();
        response.getResult().forEach(o -> codeIdMap.put(o.getCode(), o.getId()));
        return codeIdMap;
    }

    @Override
    public Map<String, OrganizationSDO> baselineGetOrganizationByIds(List<String> ids) {
        if(CollectionUtils.isEmpty(ids)) {
            return Maps.newHashMap();
        }
        BaselineGetOrganizationByIdsAllRequest request = new BaselineGetOrganizationByIdsAllRequest();
        request.setIds(ids);
        request.setStatus(1);
        BaselineGetOrganizationByIdsAllResponse response = baseDataService.baselineGetOrganizationByIdsAll(request);
        if (Objects.isNull(response) || CollectionUtils.isEmpty(response.getResult())) {
            return Maps.newHashMap();
        }

        Map<String, OrganizationSDO> result = new HashMap<>();
        response.getResult().forEach(o -> result.put(o.getId(), o));
        return result;
    }

    @Override
    public List<DictionaryResponse> queryDictionarys(String group) {
        if (StringUtils.isBlank(group)) {
            return Lists.newArrayList();
        }
        try {
            CommonDictionarySelectRequest queryDictionaryRequest = new CommonDictionarySelectRequest();
            queryDictionaryRequest.setCode(group);
            Result<List<DictionaryResponse>> response = mnOrderCenterService.commonDictionarySelect(queryDictionaryRequest);
            if (Objects.isNull(response) || CollectionUtils.isEmpty(response.getResult())) {
                return Lists.newArrayList();
            }
            return response.getResult();
        } catch (Exception e) {
            log.error("BaseDataServiceAdapterImpl#queryDictionarys error", e);
        }
        return Lists.newArrayList();
    }

    @Override
    public Map<String, Modifiers> queryEmployeeMap(List<String> ids) {
        if(CollectionUtils.isEmpty(ids)){
            return new HashMap<>();
        }
        BatchQueryEmployeeByConditionRequest queryEmployeeRequest = new BatchQueryEmployeeByConditionRequest();
        queryEmployeeRequest.setIdList(ids);
        Result<List<Modifiers>> response = mnOrderCenterService.batchQueryEmployeeByCondition(queryEmployeeRequest);
        if(response==null || CollectionUtils.isEmpty(response.getResult())){
            return new HashMap<>();
        }
        Map<String,Modifiers> result = response.getResult().stream().collect(Collectors.toMap(Modifiers::getId, Function.identity(),(v1, v2)->v1));
        return result;
    }


}
